import PathFinder from "./path/pathfinder";
import loadJson from "../../utils/loadJson";
import Objects from "../../utils/objects";
import Log from "../../utils/log";
import LayerManager from "@/plugins/mev/layer/LayerManager";

function ShortestPath() {
    this._viewer = window.cesiumViewer;
    if (ShortestPath._HANDLER == null) {
        ShortestPath._HANDLER = new Cesium.ScreenSpaceEventHandler(this._viewer.canvas);
    }
    this._startPoint = null;
    this._endPoint = null;
    this._endPointEntity = null;
    this._startPointEntity = null;
    this._pathEntity = null;
    this._pathFinder = null;
    let ls = LayerManager.getInstance().getVisibleLineLayer();
    /*let ls = null;*/
    this.layers = ls;
    this.selectedLayer = null;
}

ShortestPath.prototype.test = function() {
    //window.Cesium.loadJson('/mydata/road.geojson').then(function(geojson) {
    let that = this;

    loadJson("/file/road.geojson", function(geojson) {
        var pathFinder = new PathFinder(geojson);
        var path = pathFinder.findPath(turf.point([101.69, 33.56]), turf.point([118.057, 26.7]));
        //var ps = [[0,0],[100,30]];
        var ps = [];
        path.path.forEach(function(e) {
            var cp = Cesium.Cartesian3.fromDegrees(e[0], e[1]);
            ps.push(cp);
        });
        that._viewer.entities.add(
            new Cesium.Entity({
                id: (new Date().getTime() + (Math.random() * 1000).toFixed(0)).toString(),
                polyline: {
                    positions: new Cesium.CallbackProperty(() => ps, false),
                    show: true,
                    //distanceDisplayCondition: Number.POSITIVE_INFINITY,
                    material: Cesium.Color.GREEN,
                    width: 3,
                    clampToGround: false
                }
            })
        );
    });
};
ShortestPath.prototype.clear = function() {
    this._endPoint = null;
    this._startPoint = null;
    this._viewer.entities.remove(this._endPointEntity);
    this._viewer.entities.remove(this._startPointEntity);
    this._viewer.entities.remove(this._pathEntity);
    this._endPointEntity = null;
    this._startPointEntity = null;
    this._pathEntity = null;
    this._pathFinder = null;
};
ShortestPath.prototype.execimp = function() {
    let that = this;
    var path = that._pathFinder.findPath(turf.point(that._startPoint), turf.point(that._endPoint));
    //var ps = [[0,0],[100,30]];
    var ps = [];
    ps.push(Cesium.Cartesian3.fromDegrees(that._startPoint[0], that._startPoint[1]));
    path.path.forEach(function(e) {
        var cp = Cesium.Cartesian3.fromDegrees(e[0], e[1]);
        ps.push(cp);
    });
    ps.push(Cesium.Cartesian3.fromDegrees(that._endPoint[0], that._endPoint[1]));
    that._pathEntity = new Cesium.Entity({
        id: (new Date().getTime() + (Math.random() * 1000).toFixed(0)).toString(),
        polyline: {
            positions: new Cesium.CallbackProperty(() => ps, false),
            show: true,
            //distanceDisplayCondition: Number.POSITIVE_INFINITY,
            material: Cesium.Color.RED,
            width: 3,
            clampToGround: false
        }
    });
    that._viewer.entities.add(that._pathEntity);
};
ShortestPath.prototype.exec = function() {
    let that = this;
    if (that._startPoint == null || that._endPoint == null) {
        return "请选择点";
    }
    if (!this._geojson) {
        return "请选择图层";
    }
    if (that._pathFinder === null) {
        that._pathFinder = new PathFinder(this._geojson);
        that.execimp();
    }
};

ShortestPath.prototype.close = function() {
    if (ShortestPath._HANDLER) {
        ShortestPath._HANDLER.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_CLICK);
        ShortestPath._HANDLER.destroy();
        ShortestPath._HANDLER = null;
    }
    this.clear();
};

ShortestPath._HANDLER = null;
ShortestPath.prototype.start = function() {
    let that = this;
    if (!this.selectedLayer) return "请选择图层;";
    /*  let geojson = LayerManager.getInstance().getGeojsonData(this.selectedLayer);
    if (!geojson) return "没有加载图层数据;";
    this._geojson = geojson;*/

    Cesium.Resource.fetchJson(that.selectedLayer).then(jsonData => {
        that._geojson = jsonData;
    });

    ShortestPath._HANDLER.setInputAction(function(event) {
        if (!Cesium.Entity.supportsPolylinesOnTerrain(that._viewer.scene)) {
            console.log("当前浏览器不支持地形图");
            return;
        }
        var earthPosition = that._getCartesian3FromPosition(event.position); //获取到地形图上面的坐标
        var ellipsoid = that._viewer.scene.globe.ellipsoid;
        if (Cesium.defined(earthPosition)) {
            if (that._startPoint == null) {
                that._startPointEntity = that.createPoint(earthPosition);
                let wgs84 = ellipsoid.cartesianToCartographic(earthPosition);
                that._startPoint = [Cesium.Math.toDegrees(wgs84.longitude), Cesium.Math.toDegrees(wgs84.latitude)];
            } //调用创建点的方法
            else if (that._endPoint == null) {
                that._endPointEntity = that.createPoint(earthPosition);
                let wgs84 = ellipsoid.cartesianToCartographic(earthPosition);
                that._endPoint = [Cesium.Math.toDegrees(wgs84.longitude), Cesium.Math.toDegrees(wgs84.latitude)];
            }
        }
    }, Cesium.ScreenSpaceEventType.LEFT_CLICK);
};

ShortestPath.prototype.createPoint = function(p) {
    return this._viewer.entities.add(
        new Cesium.Entity({
            position: p,
            point: {
                color: Cesium.Color.SKYBLUE,
                pixelSize: 10,
                outlineColor: Cesium.Color.YELLOW,
                outlineWidth: 3
            }
        })
    );
};

ShortestPath.prototype._getCartesian3FromPosition = function(position) {
    let ray = this._viewer.camera.getPickRay(position);
    if (Objects.isNull(ray)) {
        return null;
    }
    return this._viewer.scene.globe.pick(ray, this._viewer.scene);
};
export default ShortestPath;
